package managedbean;

import entity.LineItem;
import entity.Product;
import entity.SaleLead.CustomerType;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.CustomerSessionBeanLocal;
import session.stateless.PurchaseOrderBeanLocal;

@ManagedBean(name="addPOWithoutSaleLead")
@ViewScoped

public class AddPOWoSaleLeadManagerBean implements Serializable {
    
    public AddPOWoSaleLeadManagerBean () {
    }
    
    @EJB
    private PurchaseOrderBeanLocal purchaseOrderBeanLocal;
    @EJB
    private CustomerSessionBeanLocal customerSessionBeanLocal;
    
    private Long purchaseOrderId;
    private Long customerId;
    private Long saleQuotationId;
    private List<LineItem> lineItems;
    private List<Integer> quantities;
    
    private String name; 
    private String NRIC;
    private String email;
    private String address;
    private String contactNumber;
    private CustomerType customerType;

    private String statusMessage;

    public void initView(PhaseEvent event)
    {
        try
        {
            if(lineItems == null)
            {
                List<Product> products = purchaseOrderBeanLocal.getProduct();
                lineItems = new ArrayList<LineItem>();

                for(Product product:products)
                {
                    LineItem lineItem = new LineItem();
                    lineItem.setProduct(product);

                    lineItems.add(lineItem);
                }
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public Long getPurchaseOrderId() {
        return purchaseOrderId;
    }

    public void setPurchaseOrderId(Long purchaseOrderId) {
        this.purchaseOrderId = purchaseOrderId;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public PurchaseOrderBeanLocal getPurchaseOrderBeanLocal() {
        return purchaseOrderBeanLocal;
    }

    public void setPurchaseOrderBeanLocal(PurchaseOrderBeanLocal purchaseOrderBeanLocal) {
        this.purchaseOrderBeanLocal = purchaseOrderBeanLocal;
    }

    public List<Integer> getQuantities() {
        return quantities;
    }

    public void setQuantities(List<Integer> quantities) {
        this.quantities = quantities;
    }

    public Long getSaleQuotationId() {
        return saleQuotationId;
    }

    public void setSaleQuotationId(Long saleQuotationId) {
        this.saleQuotationId = saleQuotationId;
    }

    public String getNRIC() {
        return NRIC;
    }

    public void setNRIC(String NRIC) {
        this.NRIC = NRIC;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public CustomerType getCustomerType() {
        return customerType;
    }

    public void setCustomerType(CustomerType customerType) {
        this.customerType = customerType;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public CustomerSessionBeanLocal getCustomerSessionBeanLocal() {
        return customerSessionBeanLocal;
    }

    public void setCustomerSessionBeanLocal(CustomerSessionBeanLocal customerSessionBeanLocal) {
        this.customerSessionBeanLocal = customerSessionBeanLocal;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
 
    public void saveNewPurchaseOrder(ActionEvent event)
    {
        String temp_name = String.valueOf(name);
        String temp_NRIC = String.valueOf(NRIC);
        String temp_email = String.valueOf(email);
        String temp_address = String.valueOf(address);
        String temp_contactNumber = String.valueOf(contactNumber);
        CustomerType temp_customerType = customerType;
        List<LineItem> temp_lineItems = lineItems;    
        Long temp_customerId;
        try
        {              
            System.out.println("a");
            temp_customerId = customerSessionBeanLocal.createNewCustomer(temp_name, temp_NRIC, temp_email, temp_address, temp_contactNumber, temp_customerType);
            System.out.println("b" + temp_customerId);
            purchaseOrderId = purchaseOrderBeanLocal.createPOWithoutSQ(temp_customerId, temp_lineItems);
            System.out.println("c");
            statusMessage = "New Purchase Order Saved Successfully.";
            
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Purchase Order Result: " + statusMessage + " (New Purchase Order ID: " + purchaseOrderId + ")", ""));
        }
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
        
    }
}
